<template>
	<div class="app-container">
		<div style="margin: 30px 0 0px 0;text-align: right;">
			<el-button type="warning" plain icon="el-icon-check" :disabled="canSubmit" size="mini" @click="submitCheck" v-hasPermi="['teacher:achievementcheck:add']">提交今年成果绩效</el-button>
		</div>
		<el-tabs type="border-card">
			<el-tab-pane label="论文成果">
				<thesis @canSubmit="getSubmit"></thesis>
			</el-tab-pane>
			<el-tab-pane label="课题研究">
				<research @canSubmit="getSubmit"></research>
			</el-tab-pane>
			<el-tab-pane label="工作案例">
				<workexample @canSubmit="getSubmit"></workexample>
			</el-tab-pane>
			<el-tab-pane label="指导参赛">
				<guidance @canSubmit="getSubmit"></guidance>
			</el-tab-pane>
			<el-tab-pane label="个人参赛">
				<contest @canSubmit="getSubmit"></contest>
			</el-tab-pane>
			<el-tab-pane label="个人荣誉">
				<honor @canSubmit="getSubmit"></honor>
			</el-tab-pane>

		</el-tabs>

		<!-- <div slot="footer" class="dialog-footer">
     <el-button type="primary" @click="submitForm">保 存</el-button>
     <el-button @click="cancel">重 置</el-button>
   </div> -->

	</div>
</template>

<script>
	import {
		listBasicmessage,
		getBasicmessage,
		delBasicmessage,
		addBasicmessage,
		updateBasicmessage
	} from "@/api/teacher/basicmessage";
	import {
		getUserProfile
	} from "@/api/system/user";
	
	//辅导员论文成果组件
	import thesis from '@/views/teacher/basicmessage/thesis/index.vue'
	//辅导员课题研究组件
	import research from '@/views/teacher/basicmessage/research/index.vue'
	//辅导员工作案例组件
	import workexample from '@/views/teacher/basicmessage/workexample/index.vue'
	//辅导员指导参赛组件
	import guidance from '@/views/teacher/basicmessage/guidance/index.vue'
	//辅导员个人参赛组件
	import contest from '@/views/teacher/basicmessage/contest/index.vue'
	//辅导员个人荣誉组件
	import honor from '@/views/teacher/basicmessage/honor/index.vue'

	import {
			listAchievementcheck,
			getAchievementcheck,
			addAchievementcheck,
			updateAchievementcheck,
			delAchievementcheck
		} from "@/api/teacher/achievementcheck";
	//获取学院、年级、班级信息
	import { listClass, getClass, delClass, addClass, updateClass, getMajorsName } from "@/api/stuCQS/basedata/class";
	import { listGrade, getGrade, queryGrade } from "@/api/stuCQS/basedata/grade";
	import { getDeptName } from "@/api/system/dept";
	export default {
		name: "achievementFill",
		dicts: ['rt_nature', 'rt_political', 'rt_nation', 'rt_college', 'rt_grade', 'rt_classes','rt_filling_college'],
		components: {
			thesis,
			research,
			workexample,
			guidance,
			contest,
			honor
		},
		data() {
			return {
				//用户信息
				user: {},
				//是否允许设置年级、学院
				setGrade:true,
				grade:[],
				setClass:true,
				class:[],
				//是否允许提交
				canSubmit:false,
				// 当前辅导员ID
				currentId: null,
				//当前辅导员姓名、学院
				currentName:null,
				currentCollege:null,
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 子表选中数据
				checkedSysTeacherTakeclass: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// basicMessage表格数据
				basicmessageList: [],
				// takeClass表格数据
				sysTeacherTakeclassList: [],
				// study表格数据
				studyList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					name: null,
					gender: null,
					birthday: null,
					nation: null,
					photo: null,
					political: null,
					wechat: null,
					address: null,
					identity: null,
					qqnumber: null,
					marriage: null,
					anotherDuties: null,
					level2units: null,
					serviceHours: null,
					jobNature: null,
					joinWorkHours: null,
					highestEducation: null,
					educated: null,
					study: null,
					lastestEducation: null,
					awardingInstitutions: null,
					email: null,
					college:null,

				},
				// 表单参数
				form: {},
				// 表单校验
				rules: {},
				collegeList:[],//学院列表
				gradeList: [],//年级列表
				classList: [],//班级列表
			};
		},
		created() {
			this.getList();
			this.getUser();
		},
		methods: {
			/** 查询basicMessage列表 */
			getList() {
				//获取学院名称列表
				getDeptName().then(res => {
				    this.collegeList = res.data;
					
				})
				
				//获取年级名称列表
				listGrade().then(res => {
				    this.gradeList = res.rows;
					
				})
				
				
				
				this.loading = true;
				listBasicmessage(this.queryParams).then(response => {
					this.basicmessageList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					teacherId: null,
					name: null,
					gender: null,
					birthday: null,
					nation: null,
					photo: null,
					political: null,
					wechat: null,
					address: null,
					identity: null,
					qqnumber: null,
					marriage: null,
					anotherDuties: null,
					level2units: null,
					serviceHours: null,
					jobNature: null,
					joinWorkHours: null,
					highestEducation: null,
					educated: null,
					study: null,
					lastestEducation: null,
					awardingInstitutions: null,
					email: null,

				};
				this.sysTeacherTakeclassList = [];
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.teacherId)
				this.single = selection.length !== 1
				this.multiple = !selection.length
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset();
				this.open = true;
				this.title = "添加基础信息";
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				const teacherId = row.teacherId || this.ids
				getBasicmessage(teacherId).then(response => {
					this.form = response.data;
					this.sysTeacherTakeclassList = response.data.sysTeacherTakeclassList;
					this.open = true;
					this.title = "修改基础信息";
				});
			},
			/** 提交按钮 */
			submitForm() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						this.form.sysTeacherTakeclassList = this.sysTeacherTakeclassList;
						if (this.title == "修改基础信息") {
							updateBasicmessage(this.form).then(response => {
								this.$modal.msgSuccess("修改成功");
								
								this.open = false;
								this.getList();
								this.getUser();
							});
						} else if(this.title == "添加基础信息"){
							
							addBasicmessage(this.form).then(response => {
								this.$modal.msgSuccess("执行成功");
								
								this.open = false;
								this.getList();
								this.getUser();
							});
						}
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const teacherIds = row.teacherId || this.ids;
				this.$modal.confirm('是否确认删除基础信息编号为"' + teacherIds + '"的数据项？').then(function() {
					return delBasicmessage(teacherIds);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			},
			/** takeClass序号 */
			rowSysTeacherTakeclassIndex({
				row,
				rowIndex
			}) {
				row.index = rowIndex + 1;
			},
			/** takeClass添加按钮操作 */
			handleAddSysTeacherTakeclass() {
				let obj = {};
				obj.college = "";
				obj.grade = "";
				obj.classteam = "";
				obj.number = "";
				obj.setGrade=true;
				obj.setClass=true;
				this.sysTeacherTakeclassList.push(obj);
			},
			/** takeClass删除按钮操作 */
			handleDeleteSysTeacherTakeclass() {
				if (this.checkedSysTeacherTakeclass.length == 0) {
					this.$modal.msgError("请先选择要删除的带班信息数据");
				} else {
					const sysTeacherTakeclassList = this.sysTeacherTakeclassList;
					const checkedSysTeacherTakeclass = this.checkedSysTeacherTakeclass;
					this.sysTeacherTakeclassList = sysTeacherTakeclassList.filter(function(item) {
						return checkedSysTeacherTakeclass.indexOf(item.index) == -1
					});
				}
			},
			/** 复选框选中数据 */
			handleSysTeacherTakeclassSelectionChange(selection) {
				this.checkedSysTeacherTakeclass = selection.map(item => item.index)
			},
			/** 导出按钮操作 */
			handleExport() {
				this.download('teacher/basicmessage/export', {
					...this.queryParams
				}, `basicmessage_${new Date().getTime()}.xlsx`)
			},
			//获取用户信息
			getUser() {
				getUserProfile().then(response => {
					this.user = response.data;
					
					this.currentName=this.user.nickName;
					this.currentCollege=this.user.dept.deptName;
					var teacherIds;
					if(this.user.userName=="admin"){
						teacherIds = 1;
						this.currentId = 1;
					}else{
						teacherIds = parseInt(this.user.userName);
						this.currentId = parseInt(this.user.userName);
					}
					
			
					let userMessage = {
						teacherId:teacherIds,
						// teacherId:100,
						name:this.user.nickName,
						phone:this.user.phonenumber,
						email:this.user.email,
						gender:this.user.sex
					}
					
					getBasicmessage(teacherIds).then(response => {
						if (response.data != undefined) {
							this.form = response.data;
							this.sysTeacherTakeclassList = response.data.sysTeacherTakeclassList;
							this.open = true;
							this.title = "修改基础信息";
						} else {
							this.form=userMessage;
							
							this.open = true;
							this.title = "添加基础信息";
						}
					});
				});

			},
			getClass(row){
				
				var c = this;
				if(row.grade!=null){
					row.setClass=false;
				}else{
					row.setGrade=true;
					row.setClass=true;
				}
				c.classList=[];
				
				var take= {
					pageSize: 100,
				    majorId: null,
				    className: null,
				    deptId: null,
				    status: null,
				    gradeId: null,
				};
				var gId = {
					gradeName:row.grade
				}
				queryGrade(gId).then(response => {
					take.gradeId = response.rows[0].gradeId;
					listClass(take).then(response => {
						for(var i =0;i<take.pageSize;i++){
							// console.log(response.rows[i].dept.deptName);
							if(row.college==response.rows[i].dept.deptName){
								c.classList.push({
									classId:response.rows[i].classId,
									className:response.rows[i].className
								})
							}
						}
						
					});
				});
				
				
			},
			getGrade(row){
				if(row.college!=null){
					row.setGrade=false;
					var c = this;
					c.classList=[];
					var take= {
						pageSize: 100,
					    majorId: null,
					    className: null,
					    deptId: null,
					    status: null,
					    gradeId: null,
					};
					var gId = {
						gradeName:row.grade
					}
					queryGrade(gId).then(response => {
						take.gradeId = response.rows[0].gradeId;
						listClass(take).then(response => {
							for(var i =0;i<take.pageSize;i++){
								// console.log(response.rows[i].dept.deptName);
								if(row.college==response.rows[i].dept.deptName){
									c.classList.push({
										classId:response.rows[i].classId,
										className:response.rows[i].className
									})
								}
							}
							
						});
						});
					
				}else{
					row.setGrade=true;
					row.setClass=true;
				}
			},
			submitCheck(){
				this.loading = true;
				var c = this;
				var achievementQuery = {
						        teacherId: c.form.teacherId
								// checkStatus:1
					}
				listAchievementcheck(achievementQuery).then(response => {
					c.loading = false;
					if(response.rows.length==0){
						c.$modal.confirm('确认提交今年成果绩效？').then(function() {
							var achievement = {
								checkId: null,
								        teacherId: c.currentId,
								        teacherName: c.currentName,
								        teacherCollege: c.currentCollege,
								        checkYear: new Date().getFullYear(),
										submitTime: new Date(),
										checkStatus:1
							}
							addAchievementcheck(achievement).then(response=>{
								c.$modal.msgSuccess("提交审核成功");
								c.canSubmit=true;
								c.getList();
							})
						}).then(() => {
							
						}).catch(() => {});
						
					}else{
						c.$modal.confirm('确认提交修改？').then(function() {
							var ck = response.rows[0];
							ck.checkStatus = 1;
							ck.checkPerson = "";
							ck.returnReason = "";
							updateAchievementcheck(ck).then(response => {
							c.$modal.msgSuccess("修改审核成功");
							c.canSubmit=true;
							c.getList();
							});
							
						}).then(() => {
							
						}).catch(() => {});
					}
				})
			},
			getSubmit(data){
				this.canSubmit = data.message;
			}
			
		}
	};
</script>
<style>
	.el-divider__text {
		font-size: 20px;
		font-weight: bold;
	}
</style>